﻿using Models;
using NPOI.HSSF.Record.Chart;
using OSSUtility;
using Ozone.DAL;
using Ozone.Help;
using Ozone.Models;
using Ozone.ShareData;
using ServiceStack;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using LogHelper = Ozone.Help.LogHelper;

namespace Ozone.BLL
{
    public class OperLogBLL
    {
        private static List<sys_operlog> _allsys_operlogs;

        /// <summary>
        /// 查询系统日志
        /// </summary>
        /// <param name="Starttime"></param>
        /// <param name="EndTime"></param>
        /// <param name="FID"></param>
        /// <returns></returns>
        public static List<sys_operlog> GetLogList(DateTime Starttime, DateTime EndTime, string FID)
        {
            try
            {
                Starttime = Convert.ToDateTime("2025-10-20 10:32:30");
                if (Starttime >= EndTime)
                {
                    _allsys_operlogs = new List<sys_operlog>();
                    return _allsys_operlogs;
                }
                _allsys_operlogs = OperLogDAL.GetLogList(Starttime, EndTime, FID);
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询系统日志发生异常", ex);
                _allsys_operlogs = null;
            }
            return _allsys_operlogs;
        }

        /// <summary>
        /// 添加日志信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool AddLog(sys_operlog Model)
        {
            bool IsSuccess = false;
            try
            {
                IsSuccess = OperLogDAL.AddLog(Model);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加日志异常", ex);
                IsSuccess = false;
            }
            LogHelper.Debug("添加日志异常");

            return IsSuccess;
        }


        /// <summary>
        /// 删除日志信息
        /// </summary>
        /// <param name="FID"></param>
        /// <returns></returns>
        public static bool DeleteLog(int FID)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (FID > 0)
                {
                    IsSuccess = OperLogDAL.DeleteLog(FID);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("删除日志异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool UpdateLog(sys_operlog UpdateLog)
        {
            bool IsSuccess = false;
            try
            {
                if (UpdateLog != null)
                {
                    IsSuccess = OperLogDAL.UpdateLog(UpdateLog);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("修改日志异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool AddLogAndShowPage(string LogContent)
        {
            bool IsSuccess = false;
            try
            {
                sys_operlog Model = new sys_operlog();
                Model.OperationModel = "流程日志";
                Model.OperationContentJson = LogContent;
                Model.OperaterCode = "124578";
                Model.OperaterName = "12478";
                Model.OperateRemark = "12485878";
                Model.CreateDate = DateTime.Now;
                AppLogData.Entity.addAppLog(0, Model.OperationContentJson);
                IsSuccess = OperLogDAL.AddLog(Model);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加日志异常", ex);
                IsSuccess = false;
            }
            LogHelper.Debug("添加日志异常");

            return IsSuccess;
        }

    }
}
